perm filename PART13.TXT[1,RWF] blob
sn#541598 filedate 1980-10-15 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00003 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 %Try to get program on same page
C00004 00003 The program generates values for A, B, C, D in the order:
C00007 ENDMK
C⊗;
%Try to get program on same page
Exercise
Write a PASCAL program to find four positive integers
A < B < C < D
for which B↑3 + C↑3 = A↑3 + D↑3. Another way of stating this problem
is: find a positive integer that can be represented two ways as the sum of
two cubes. You may find it useful to look for the smallest such integer, but
any number you find satisfying the above condition is acceptable. Your program
should print this integer as well as its two distinct representations.
Hint: let the outer iteration control D .
Sample Program
PROGRAM SUMOFCUBES ( OUTPUT ) ;
(* THIS PROGRAM FINDS FOUR INTEGERS: 0 < A < B < C < D
FOR WHICH A*A*A + D*D*D = B*B*B + C*C*C *)
VAR A, B, C, D : INTEGER ;
BEGIN
A := 1 ;
B := 2 ;
C := 3 ;
D := 4 ;
WHILE A*A*A + D*D*D <> B*B*B +C*C*C DO
IF A < B - 1 THEN A := A + 1
ELSE
BEGIN
A := 1 ;
IF B < C - 1 THEN B := B + 1
ELSE
BEGIN
B := 2 ;
IF C < D + 1 THEN C := C + 1
ELSE
BEGIN
C := 3 ;
D := D + 1
END
END
END ;
WRITELN ( A, D, B, C )
END.
The program generates values for A, B, C, D in the order:
A B C D
1 2 3 4
1 2 3 5
1 2 4 5
1 3 4 5
2 3 4 5
1 2 3 6
1 2 4 6
1 3 4 6
2 3 4 6
1 2 5 6
1 3 5 6
2 3 5 6
1 4 5 6
2 4 5 6
3 4 5 6
1 2 3 7
etc.
The program, upon finding A↑3 + D↑3 ≠ B↑3 + C↑3, increases the first of
A, B, C, D that can be increased without becoming equal to the next
variable; it sets all the earlier variables back to their initial values.